package com.ffcs.crmd.tsp.admin.control;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.platform.common.entity.PageInfo;
import com.ctg.itrdc.platform.common.exception.ExceptionUtil;
import com.ffcs.crmd.platform.pub.vo.RetVo;
import com.ffcs.crmd.tsp.admin.api.dto.SliceGroupDTO;
import com.ffcs.crmd.tsp.admin.api.facade.ISliceGroupFacade;

/**
 * 
 * 功能说明:
 *
 * @author ZHONGFUHUA
 * 
 * @Date 2016年2月16日 下午4:49:00
 *
 *
 *       版本号 | 作者 | 修改时间 | 修改内容
 *
 */
@Controller
@ResponseBody
@RequestMapping("/tsp/sliceGroup")
public class SliceGroupController {
    
    private final Logger      logger = LoggerFactory.getLogger(SliceGroupController.class);
    
    @Autowired
    private ISliceGroupFacade sliceGroupFacade;
    
    /**
     * 
     * 功能说明:
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/add")
    public RetVo add(ModelMap map, HttpSession httpSession, @RequestBody SliceGroupDTO sliceGroupDTO) {
        RetVo retVo = new RetVo(true);
        try {
            sliceGroupFacade.save(sliceGroupDTO);
        } catch (Exception e) {
            logger.error("SliceGroupController : add slice group exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    
    @RequestMapping(value = "/get")
    public RetVo get(ModelMap map, HttpSession httpSession, @RequestBody SliceGroupDTO sliceGroupDTO) {
        RetVo retVo = new RetVo(true);
        try {
            SliceGroupDTO rsliceGroupDTO = sliceGroupFacade.get(sliceGroupDTO);
            retVo.setObject(rsliceGroupDTO);
        } catch (Exception e) {
            logger.error("JobIdentityInfoController : get slice group exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    
    /**
     * 
     * 功能说明:
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/deleteList")
    public RetVo deleteList(ModelMap map, HttpSession httpSession, @RequestBody List<SliceGroupDTO> sliceGroupDtoList) {
        RetVo retVo = new RetVo(true);
        try {
            sliceGroupFacade.remove(sliceGroupDtoList);
        } catch (Exception e) {
            logger.error("SliceGroupController : delete slice group exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    
    /**
     * 
     * 功能说明:
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/delete")
    public RetVo delete(ModelMap map, HttpSession httpSession, @RequestBody SliceGroupDTO sliceGroupDto) {
        RetVo retVo = new RetVo(true);
        try {
            sliceGroupFacade.remove(sliceGroupDto);
        } catch (Exception e) {
            logger.error("SliceGroupController : delete slice group exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/update")
    public RetVo update(ModelMap map, HttpSession httpSession, @RequestBody SliceGroupDTO sliceGroupDTO) {
        RetVo retVo = new RetVo(true);
        try {
            sliceGroupFacade.update(sliceGroupDTO);
        } catch (Exception e) {
            logger.error("SliceGroupController : update slice group exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/query")
    public RetVo query(ModelMap map, HttpSession httpSession, @RequestBody SliceGroupDTO sliceGroupDTO) {
        RetVo retVo = new RetVo(true);
        try {
            if(StringUtils.isBlank(sliceGroupDTO.getClusterName())){
                String clusterName = httpSession.getAttribute("clusterName") == null ? null
                    : String.valueOf(httpSession.getAttribute("clusterName"));
                sliceGroupDTO.setClusterName(clusterName);
            }
            List<SliceGroupDTO> sliceGroupDTOList = sliceGroupFacade.query(sliceGroupDTO);
            retVo.setDataList(sliceGroupDTOList);
        } catch (Exception e) {
            logger.error("SliceGroupController : query slice group by job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/queryPage")
    public RetVo queryPage(ModelMap map, HttpSession httpSession, @RequestBody SliceGroupDTO sliceGroupDTO) {
        RetVo retVo = new RetVo(true);
        PageInfo<SliceGroupDTO> pageInfo = new PageInfo<SliceGroupDTO>();
        try {
            if(StringUtils.isBlank(sliceGroupDTO.getClusterName())){
                String clusterName = httpSession.getAttribute("clusterName") == null ? null
                    : String.valueOf(httpSession.getAttribute("clusterName"));
                sliceGroupDTO.setClusterName(clusterName);
            }
            pageInfo = sliceGroupFacade.queryPage(sliceGroupDTO);
            retVo.setPageInfo(pageInfo);
        } catch (Exception e) {
            logger.error("SliceGroupController : query all slice group page exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
}
